﻿Imports System.IO
Public Class frm_main
    Public Const WM_NCLBUTTONDOWN As Integer = &HA1
    Public Const HTCAPTION As Integer = &H2
    Declare Function ReleaseCapture Lib "user32.dll" () As Int32
    Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    Dim inicio
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        ListBox1.Items.Add(TimeOfDay & " - Cerrando prorama...")
        Me.Close()

    End Sub

    Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
        PictureBox1.Image = My.Resources.btn_salir_focus
    End Sub

    Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
        PictureBox1.Image = My.Resources.btn_salir_normal
    End Sub

    Private Sub PictureBox2_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox2.MouseEnter
        PictureBox2.Image = My.Resources.btn_entrar_focus
    End Sub

    Private Sub PictureBox2_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox2.MouseLeave
        PictureBox2.Image = My.Resources.btn_entrar_normal
    End Sub

    Private Sub PictureBox3_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox3.MouseEnter
        PictureBox3.Image = My.Resources.btn_host_focus
    End Sub

    Private Sub PictureBox3_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox3.MouseLeave
        PictureBox3.Image = My.Resources.btn_host_normal
    End Sub

    Private Sub frm_main_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        'move the main form 
        ReleaseCapture()
        SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
    End Sub

    Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
        Me.Hide()
        ListBox1.Items.Add(TimeOfDay & " - Iniciando control de creacion de Host...")
        frm_host.Show()
    End Sub

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        Me.Hide()
        ListBox1.Items.Add(TimeOfDay & " - Iniciando control de conexión a un Host...")
        frm_join.Show()
    End Sub

    Public Function cargame()
        ListBox1.Items.Add(TimeOfDay & " - Comprobando ruta del juego en '" & Application.StartupPath & "'")
        Dim existe, existe2
        existe = My.Computer.FileSystem.FileExists(Application.StartupPath & "\portal2.exe")
        If existe = False Then
            ListBox1.Items.Add(TimeOfDay & " - Archivo no encontrado en '" & Application.StartupPath & "'")
            inicio = GetSetting(Application.ProductName, "config", "firststart", 1)
            ListBox1.Items.Add(TimeOfDay & " - Comprobando ruta del juego en configuracion del sistema")
            If inicio = 1 Then
                ListBox1.Items.Add(TimeOfDay & " - No existe configuracion de sistema...")
                ListBox1.Items.Add(TimeOfDay & " - Abriendo dialogo de carpetas...")
                MsgBox("Por Favor selecciona la carpeta de PORTAL 2", vbInformation)
                juego()
            Else
                ListBox1.Items.Add(TimeOfDay & " - Configuracion encontrada...")
                ListBox1.Items.Add(TimeOfDay & " - Comprobando archivos...")
                existe2 = My.Computer.FileSystem.FileExists(GetSetting(Application.ProductName, "config", "gamepath") & "\portal2.exe")
                If existe2 = False Then
                    ListBox1.Items.Add(TimeOfDay & " - Archivo de configuracion incorrecto...")
                    ListBox1.Items.Add(TimeOfDay & " - No se encontró '" & GetSetting(Application.ProductName, "config", "gamepath") & "'")
                    ListBox1.Items.Add(TimeOfDay & " - Borrando variables...")
                    DeleteSetting(Application.ProductName, "config", "gamepath")
                    DeleteSetting(Application.ProductName, "config", "firststart")
                    ListBox1.Items.Add(TimeOfDay & " - Recargando configuracion...")
                    juego()
                Else
                    ListBox1.Items.Add(TimeOfDay & " - Archivo encontrado...")
                    ListBox1.Items.Add(TimeOfDay & " - Usando carpeta de juego '" & GetSetting(Application.ProductName, "config", "gamepath") & "'")
                End If
            End If
        Else
            ListBox1.Items.Add(TimeOfDay & "PORTAL2.EXE fue encontrado, guardando variables y continuando...")
            SaveSetting(Application.ProductName, "config", "firststart", 0)
            SaveSetting(Application.ProductName, "config", "gamepath", Application.StartupPath)
        End If
        ListBox1.Items.Add(TimeOfDay & " - Iniciando interfaz...")
        Return 0
    End Function
    Public Function juego()
        Dim carpeta, existe
        Try
            FolderBrowserDialog1.Description = "Selecciona la carpeta donde tienes PORTAL 2 instalado"
            FolderBrowserDialog1.ShowDialog()
            carpeta = FolderBrowserDialog1.SelectedPath
            If carpeta = "" Then
                MsgBox("Por Favor selecciona la carpeta de PORTAL 2", vbInformation)
                ListBox1.Items.Add(TimeOfDay & " - No se seleccionó una carpeta...")
                ListBox1.Items.Add(TimeOfDay & " - Abriendo dialogo de carpetas...")
                juego()
            Else
                existe = My.Computer.FileSystem.FileExists(carpeta & "\portal2.exe")
                If existe = False Then
                    MsgBox("La carpeta seleccionada no contiene el juego PORTAL 2" & vbCrLf & "Por Favor selecciona la carpeta de PORTAL 2", vbInformation)
                    ListBox1.Items.Add(TimeOfDay & " - No se encontro PORTAL2 en la carpeta seleccionada...")
                    ListBox1.Items.Add(TimeOfDay & " - Abriendo dialogo de carpetas...")
                    juego()
                Else
                    ListBox1.Items.Add(TimeOfDay & " - Comprobando ruta del juego en " & Application.StartupPath)
                    ListBox1.Items.Add(TimeOfDay & " - Juego encontrado en '" & carpeta & "'")
                    ListBox1.Items.Add(TimeOfDay & " - Guardando variables...")
                    SaveSetting(Application.ProductName, "config", "firststart", 0)
                    SaveSetting(Application.ProductName, "config", "gamepath", carpeta)
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
            ListBox1.Items.Add(TimeOfDay & " - ERROR CRITICO " & ex.Message)
        End Try
        Return 0
    End Function

    Private Sub frm_main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add(TimeOfDay & " - Ininicando programa")
        ListBox1.Items.Add(TimeOfDay & " - Iniciando Log...")
        cargame()
    End Sub
    Private Sub frm_main_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Dim existe
        Timer1.Enabled = False
        ListBox1.Items.Add(TimeOfDay & " - Cerrando programa y guardando log...")
        existe = My.Computer.FileSystem.DirectoryExists(Application.StartupPath & "\logs launcher")
        If existe = False Then
            My.Computer.FileSystem.CreateDirectory(Application.StartupPath & "\logs launcher")
        End If
        Dim oSW As New StreamWriter(Application.StartupPath & "\logs launcher\" & DateString & " - " & TimeOfDay.Hour & "_" & TimeOfDay.Minute & ".txt")
        Dim i As Integer
        oSW.WriteLine("Archivo de registro para la fecha " & Now)
        oSW.Flush()
        oSW.WriteLine("===============================================================")
        oSW.Flush()
        oSW.WriteLine("")
        oSW.Flush()
        For i = 0 To ListBox1.Items.Count - 1
            Dim Linea = ListBox1.Items(i).ToString() 'Asi obtienes cada elemento del arreglo y conviertes su valor a cadena
            oSW.WriteLine(Linea)
            oSW.Flush()
        Next
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ListBox1.SelectedIndex = ListBox1.Items.Count - 1
    End Sub
End Class